//
//  BookPlaceDetailController.m
//  FreeTrip
//
//  Created by 352324497@qq.com on 15/11/26.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "BookPlaceDetailController.h"
#import "BookPlaceDetailOneCell.h"
#import "BookPlaceDetailSectionCell.h"
#import "BookPlaceDetailModel.h"
#import "BookPlaceDetailTripModel.h"
#import "BookDetailPhotosController.h"
#import "PhotosModel.h"
#import "PhotoViewController.h"
#import "BookPlaceDetailTravelController.h"
#import "KCMainViewController.h"
@interface BookPlaceDetailController ()<BookPlaceDetailDelegate>
@property (nonatomic, strong)NSMutableArray *TripModelArray;
@property (nonatomic, strong)NSMutableArray *NotesModelArray;
@property (nonatomic, strong)BookPlaceDetailModel *model;

@property (nonatomic, strong) NSString *url;
@end
@implementation BookPlaceDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.name;
    self.tableView.separatorStyle = NO;
    self.TripModelArray = [NSMutableArray array];
    self.NotesModelArray = [NSMutableArray array];
    self.url = [NSString stringWithFormat:@"http://chanyouji.com/api/attractions/%ld.json", (long)self.Id];
    
    [[NetworkState shareInstance] reachablityStates:self];

    [[NetworkRequest shareNetworeRequest]netRequestForBookPlaceDetailWithUrl:self.url networkRequest:^(id model) {
        self.model = model;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.TripModelArray = self.model.tripArray;
            [self.tableView reloadData];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        });
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.TripModelArray.count + 1;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    if (section == 0)
    {
        return nil;
    }
    else
    {
        NSMutableArray *arr = self.TripModelArray[section - 1];
        BookPlaceDetailTripModel *model = arr[0];
        
        return model.name;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0)
    {
        return 1;
    }
    else
    {
        return [self.TripModelArray[section - 1] count];
    }
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return [BookPlaceDetailOneCell heightForCell:self.model];
    }
    else
    {
        NSMutableArray *arr = self.TripModelArray[indexPath.section - 1];
        BookPlaceDetailTripModel *model = arr[indexPath.row];
        return [BookPlaceDetailSectionCell heightForCell:model];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0)
    {
        BookPlaceDetailOneCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oneCell"];
        if (cell == nil)
        {
            cell = [[BookPlaceDetailOneCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"oneCell"];
            
        }
        cell.model = self.model;
        [cell.travelsButton addTarget:self action:@selector(travelsDetailPage:) forControlEvents:UIControlEventTouchUpInside];
        [cell.photosButton addTarget:self action:@selector(photosButtonAction) forControlEvents:UIControlEventTouchUpInside];
        [cell.mapButton addTarget:self action:@selector(mapButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
        return cell;
    }
    else
    {
        BookPlaceDetailSectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sectionCell"];
        if (cell == nil)
        {
            cell = [[BookPlaceDetailSectionCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"sectionCell"];
        }
        cell.layer.borderColor = [UIColor grayColor].CGColor;
        cell.layer.borderWidth = 0.2;
        NSMutableArray *arr = self.TripModelArray[indexPath.section - 1];
        BookPlaceDetailTripModel *model = arr[indexPath.row];
        cell.model = model;
        cell.delegate = self;
        return cell;
    }
}

#pragma mark -- 游记跳转 --
- (void)travelsDetailPage:(UIButton *)button
{
    BookPlaceDetailTravelController *travel = [[BookPlaceDetailTravelController alloc]init];
    travel.url = self.url;
    [self.navigationController pushViewController:travel animated:NO];
}

#pragma mark - 图片跳转
- (void)photosButtonAction
{
    BookDetailPhotosController *photoVc = [[BookDetailPhotosController alloc] init];
    photoVc.Id = self.Id;
    [self.navigationController pushViewController:photoVc animated:YES];
}

#pragma mark - 地图跳转
-(void)mapButtonAction
{
    KCMainViewController *KCMainVC = [[KCMainViewController alloc]init];
//    KCMainVC.entry_name = self.name;
//    KCMainVC.name_zh_cn = self.name_zh_cn;
//    KCMainVC.longitude = self.longitude;
//    KCMainVC.latitude = self.latitude;
    KCMainVC.Id = self.Id;
    KCMainVC.isDetail = YES;
    [self.navigationController pushViewController:KCMainVC animated:YES];
}
#pragma mark -- collectionView跳转 --
- (void)JumpPageWithArray:(NSMutableArray *)array item:(NSInteger)item
{
    PhotoViewController *photoVc = [[PhotoViewController alloc]init];
    photoVc.modelArray = array;
    photoVc.item = item;
    [self presentViewController:photoVc animated:NO completion:nil];
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
